package com.ynny.dao;

import com.ynny.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class UserDaoImpl implements UserDao{
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public User selectUserByUserName(String userName) {
        String sql = "select * from user where number = ?";
        List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), userName);
      if (users.size() == 0){
          return null;
      }
      return users.get(0);

    }

    @Override
    public int insertUser(User user) {
        String sql = "insert into user(number,password,identity) values(?,?,?)";
        int i = jdbcTemplate.update(sql,
                user.getNumber(),
                user.getPassword(),
                user.getIdentity());


        return i;
    }
}
